define(['jquery', 'bootstrap', 'backend', 'template', 'table', 'form'], function ($, undefined, Backend, Template, Table, Form) {
    var Controller = {
        index: function () {
            var form_id = $('#form_id').val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'formdesign/foreign/index?form_id=' + form_id,
                    add_url: 'formdesign/foreign/add?form_id=' + form_id,
                    edit_url: 'formdesign/foreign/edit?form_id=' + form_id,
                    del_url: 'formdesign/foreign/del?form_id=' + form_id,
                    multi_url: 'formdesign/foreign/multi?form_id=' + form_id,
                    table: 'foreign'
                }
            });
            var table = $("#table");
            var columns = [
                { checkbox: true },
                { field: 'id', title: __('序号'), operate: false },
                { field: 'name', title: __('记录名称'), align: 'center' },
            ];
            $.each(Config.columns, function (i, j) {
                var data = { field: j.name, title: j.title, operate: 'like' };
                if (j.type === 'image' || j.type === 'images') {
                    data.formatter = Table.api.formatter.image;
                    data.operate = false;
                } else if (j.type === 'images') {
                    data.formatter = Table.api.formatter.images;
                    data.operate = false;
                } else if (j.type === 'upload' || j.type === 'uploads') {
                    data.formatter = Table.api.formatter.url;
                    data.operate = false;
                } else if (j.type === 'switcher') {
                    data.formatter = Controller.api.formatter.switcher;
                    data.searchList = { '1': '开启', '0': '关闭' };
                } else if (j.type === 'daterange' || j.type === 'datetimerange' || j.type === 'timepicker' || j.type === 'timerange') {
                    data.operate = false;
                } else if (j.type === 'datepicker' || j.type === 'datetimepicker') {
                    data.operate = 'RANGE';
                    data.addclass = 'datetimerange';
                } else if (
                    j.type === 'radios' ||
                    j.type === 'checkboxs' ||
                    j.type === 'select' ||
                    j.type === 'selects' ||
                    j.type === 'selectpicker' ||
                    j.type === 'selectpickers') {
                    data.formatter = Controller.api.formatter.content;
                    data.extend = j.list.replace(/&quot;/g, '"');
                    data.searchList = JSON.parse(data.extend);
                }
                columns.push(data);
            });
            columns.push({ field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate });
            $('.btn-query').click(function () {
                Fast.api.open(
                    'formdesign/foreign/query?form_id=' + form_id,
                    '字段选择', {
                        callback: function (value) {
                            window.location.reload();
                        }
                    }
                );
            });

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'foreign.id',
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // escape: false,
                columns: columns,
                search: false,
                searchFormVisible: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        query: function () {
            require(['doublebox'], function () {
                var $doublebox = $('.doublebox-query');
                var options = $doublebox.data();
                $doublebox.height(options.height ? options.height : 200);
                var url = $doublebox.data('source');
                var value = $doublebox.attr('value').toString();
                var keyField = 'id';
                var showField = 'name';
                var arrayValue = [];
                if (url !== '' && typeof (url) !== undefined && $.trim(url).length > 0) {
                    $.get(url, {
                        showField: showField,
                        keyField: keyField
                    }, function (data) {
                        if (value !== '' && typeof (value) !== undefined && $.trim(value).length > 0) {
                            value = value.split(',');
                            $.each(value, function (key, val) {
                                for (let i = 0; i < data.length; i++) {
                                    var element = data[i];
                                    if (element[keyField].toString() === val.toString()) {
                                        arrayValue.push(element);
                                        data.splice(i--, 1);
                                    }
                                }
                            })
                        }
                        var defaults = {
                            nonSelectedListLabel: '不显示',
                            selectedListLabel: '显示',
                            filterTextClear: "显示全部",
                            filterPlaceHolder: "过滤",
                            moveSelectedLabel: "添加选中",
                            moveAllLabel: "添加所有",
                            removeSelectedLabel: "移除选中",
                            removeAllLabel: "移除所有",
                            preserveSelectionOnMove: 'moved',
                            moveOnSelect: true,
                            nonSelectedList: data,
                            selectedList: arrayValue,
                            optionValue: keyField,
                            optionText: showField,
                            doubleMove: true
                        };
                        var options = $.extend(defaults, options);
                        $doublebox.doublebox(options);
                    })
                }
            });
            Form.api.bindevent($("form[role=form]"), function () {
                Fast.api.close();
            });
        },
        api: {
            formatter: {
                switcher: function (value, row, index) {
                    return value ? '开启' : '关闭';
                },
                content: function (value, row, index) {
                    if (!value) {
                        return '';
                    }
                    if (!this.extend) {
                        return value;
                    }
                    var valueArr = value.toString().split(/,/);
                    var extend = JSON.parse(this.extend); //转换为json对象
                    var result = [];
                    $.each(valueArr, function (i, j) {
                        result.push(typeof extend[j] !== 'undefined' ? extend[j] : j);
                    });
                    return result.join(',');
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});